unit UltimateSheduler_ActionForm;

interface

uses
  UltimateSheduler_Defines,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, JvToolEdit, StdCtrls, Buttons, Mask, JvExMask, ExtCtrls, XPMan;

type
  TUSActionForm = class(TForm)
    RB_Copy: TRadioButton;
    RB_Move: TRadioButton;
    DE: TJvDirectoryEdit;
    RB_Launch: TRadioButton;
    E_Param: TEdit;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    FE_Launch: TJvFilenameEdit;
    Label1: TLabel;
    CBX_Category: TComboBox;
    RB_Start: TRadioButton;
    CB_Enabled: TCheckBox;
    CB_Wait: TCheckBox;
    CB_First: TCheckBox;
    RB_Create: TRadioButton;
    RB_Delete: TRadioButton;
    FE_Flag: TJvFilenameEdit;
    E_Rename: TEdit;
    RB_Rename: TRadioButton;
    RG_Exists: TRadioGroup;
    CB_SaveName: TCheckBox;
    CB_SavePath: TCheckBox;
    XPManifest1: TXPManifest;
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure CB_EnabledClick(Sender: TObject);
    procedure RB_CopyClick(Sender: TObject);
    procedure RB_MoveClick(Sender: TObject);
    procedure RB_LaunchClick(Sender: TObject);
    procedure RB_StartClick(Sender: TObject);
    procedure RB_CreateClick(Sender: TObject);
    procedure RB_DeleteClick(Sender: TObject);
    procedure RB_RenameClick(Sender: TObject);
    procedure RG_ExistsClick(Sender: TObject);
    procedure DEChange(Sender: TObject);
    procedure FE_LaunchChange(Sender: TObject);
    procedure CB_SavePathClick(Sender: TObject);
    procedure CB_WaitClick(Sender: TObject);
    procedure E_ParamChange(Sender: TObject);
    procedure CBX_CategoryChange(Sender: TObject);
    procedure CB_FirstClick(Sender: TObject);
    procedure FE_FlagChange(Sender: TObject);
    procedure E_RenameChange(Sender: TObject);
    procedure CB_SaveNameClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    FAction: anAction;
    procedure SetLanguage(language: String);
    procedure SetAction(AAction: anAction);
    procedure SetCategories;
    procedure CheckAction(Other: Boolean = true);
  public
    { Public declarations }
    property Language: String write SetLanguage;
    property myAction: anAction read FAction write SetAction;
  end;

implementation

{$R *.dfm}

uses
 gnugettext,
 Locales;

var
 CanSetAct: Boolean = true;

procedure TUSActionForm.SetLanguage(language: String);
begin
 TranslateForm(Self, Language);
end;

procedure TUSActionForm.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
 if ModalResult=mrOK then
  begin
   if FAction.ActType=0 then
    begin
     MessageDlg(_('Please select type of action!'), mtError, [mbOK], 0);
     CanClose:=false;
     Exit;
    end;
   if not (FAction.Existing in [0..RG_Exists.Items.Count-1]) then
    begin
     MessageDlg(_('Please select type of existing file process!'), mtError, [mbOK], 0);
     ActiveControl:=RG_Exists;
     CanClose:=false;
     Exit;
    end;
   if RB_Copy.Checked or RB_Move.Checked then
    begin
     if not DirectoryExists(FAction.Destination) then
      begin
       MessageDlg(_('Destination folder must exist!'), mtError, [mbOK], 0);
       ActiveControl:=DE;
       CanClose:=false;
       Exit;
      end;
    end;
   if RB_Launch.Checked then
    begin
     if not FileExists(FE_Launch.Text) then
      begin
       MessageDlg(_('File to launch must exist!'), mtError, [mbOK], 0);
       ActiveControl:=FE_Launch;
       CanClose:=false;
       Exit;
      end;
    end;
  end;
end;

procedure TUSActionForm.SetAction(AAction: anAction);
begin
 FAction.Assign(AAction);
 //
 CanSetAct:=false;
 CheckAction(false);
 CanSetAct:=true;
end;

procedure TUSActionForm.CheckAction(Other: Boolean = true);
 var
  i: Integer;
begin
 CB_Enabled.Checked:=FAction.Enabled;
 //
 RB_Copy.Checked:=FAction.ActType=actCopy;
 RB_Move.Checked:=FAction.ActType=actMove;
 DE.Enabled:=RB_Copy.Checked or RB_Move.Checked;
 if DE.Enabled then
  begin
   if Other then
     DE.Text:=''
   else
     DE.Text:=FAction.Destination;
  end
 else
   DE.Text:='';
 CB_SavePath.Enabled:=RB_Copy.Checked or RB_Move.Checked;
 if CB_SavePath.Enabled then
  begin
   if Other then
     CB_SavePath.Checked:=false
   else
     CB_SavePath.Checked:=FAction.Flag;
  end
 else
   CB_SavePath.Checked:=false;
 //
 RB_Launch.Checked:=FAction.ActType=actLaunch;
 FE_Launch.Enabled:=RB_Launch.Checked;
 if FE_Launch.Enabled then
  begin
   if Other then
     FE_Launch.Text:=''
   else
     FE_Launch.Text:=FAction.Destination;
  end
 else
   FE_Launch.Text:='';
 CB_Wait.Enabled:=RB_Launch.Checked;
 if CB_Wait.Enabled then
  begin
   if Other then
     CB_Wait.Checked:=false
   else
     CB_Wait.Checked:=FAction.Flag;
  end
 else
   CB_Wait.Checked:=false;
 Label1.Enabled:=RB_Launch.Checked;
 E_Param.Enabled:=RB_Launch.Checked;
 if E_Param.Enabled then
  begin
   if Other then
     E_Param.Text:=''
   else
     E_Param.Text:=FAction.Parameter;
  end
 else
   E_Param.Text:='';
 //
 RB_Start.Checked:=FAction.ActType=actStart;
 CBX_Category.Enabled:=RB_Start.Checked;
 if CBX_Category.Enabled then
  begin
   if Other then
     i:=0
   else
    begin
     i:=IndexOfValue(FAction.Destination, CBX_Category.Items);
     if i<0 then
       i:=0;
    end;
   CBX_Category.ItemIndex:=i;
   if i=0 then
     FAction.Destination:=CBX_Category.Text;
  end
 else
   CBX_Category.ItemIndex:=0;
 CB_First.Enabled:=RB_Start.Checked;
 if CB_First.Enabled then
  begin
   if Other then
     CB_First.Checked:=false
   else
     CB_First.Checked:=FAction.Flag;
  end
 else
   CB_First.Checked:=false;
 //
 RB_Create.Checked:=FAction.ActType=actFlagCreate;
 RB_Delete.Checked:=FAction.ActType=actFlagDelete;
 FE_Flag.Enabled:=RB_Create.Checked or RB_Delete.Checked;
 if FE_Flag.Enabled then
  begin
   if Other then
     FE_Flag.Text:=''
   else
     FE_Flag.Text:=FAction.Destination;
  end
 else
   FE_Flag.Text:='';
 //
 RB_Rename.Checked:=FAction.ActType=actRename;
 E_Rename.Enabled:=RB_Rename.Checked;
 if E_Rename.Enabled then
  begin
   if Other then
     E_Rename.Text:=''
   else
     E_Rename.Text:=FAction.Destination;
  end
 else
   E_Rename.Text:='';
 CB_SaveName.Enabled:=RB_Rename.Checked;
 if CB_SaveName.Enabled then
  begin
   if Other then
     CB_SaveName.Checked:=false
   else
     CB_SaveName.Checked:=FAction.Flag;
  end
 else
   CB_SaveName.Checked:=false;
 //
 RG_Exists.ItemIndex:=FAction.Existing;
end;

procedure TUSActionForm.FormCreate(Sender: TObject);
begin
 TranslateComponent(Self);
 //
 FAction:=anAction.Create(nil);
 //
 SetCategories;
end;

procedure TUSActionForm.SetCategories;
 var
  i: Integer;
begin
 CBX_Category.Items.Clear;
 for i:=0 to CatList.Count-1 do
   CBX_Category.Items.Add(CatList.ValueFromIndex[i]);
end;

procedure TUSActionForm.CB_EnabledClick(Sender: TObject);
begin
 FAction.Enabled:=CB_Enabled.Checked;
end;

procedure TUSActionForm.RB_CopyClick(Sender: TObject);
begin
 if CanSetAct then
  begin
   FAction.ActType:=actCopy;
   CheckAction();
  end;
end;

procedure TUSActionForm.RB_MoveClick(Sender: TObject);
begin
 if CanSetAct then
  begin
   FAction.ActType:=actMove;
   CheckAction();
  end;
end;

procedure TUSActionForm.RB_LaunchClick(Sender: TObject);
begin
 if CanSetAct then
  begin
   FAction.ActType:=actLaunch;
   CheckAction();
  end;
end;

procedure TUSActionForm.RB_StartClick(Sender: TObject);
begin
 if CanSetAct then
  begin
   FAction.ActType:=actStart;
   CheckAction();
  end;
end;

procedure TUSActionForm.RB_CreateClick(Sender: TObject);
begin
 if CanSetAct then
  begin
   FAction.ActType:=actFlagCreate;
   CheckAction();
  end;
end;

procedure TUSActionForm.RB_DeleteClick(Sender: TObject);
begin
 if CanSetAct then
  begin
   FAction.ActType:=actFlagDelete;
   CheckAction();
  end;
end;

procedure TUSActionForm.RB_RenameClick(Sender: TObject);
begin
 if CanSetAct then
  begin
   FAction.ActType:=actRename;
   CheckAction();
  end;
end;

procedure TUSActionForm.RG_ExistsClick(Sender: TObject);
begin
 FAction.Existing:=RG_Exists.ItemIndex;
end;

procedure TUSActionForm.DEChange(Sender: TObject);
begin
 if CanSetAct then
   FAction.Destination:=TrimQuotes(DE.Text);
end;

procedure TUSActionForm.CB_SavePathClick(Sender: TObject);
begin
 if CanSetAct then
   FAction.Flag:=CB_SavePath.Checked;
end;

procedure TUSActionForm.FE_LaunchChange(Sender: TObject);
begin
 if CanSetAct then
   FAction.Destination:=FE_Launch.Text;
end;

procedure TUSActionForm.CB_WaitClick(Sender: TObject);
begin
 if CanSetAct then
   FAction.Flag:=CB_Wait.Checked;
end;

procedure TUSActionForm.E_ParamChange(Sender: TObject);
begin
 if CanSetAct then
   FACtion.Parameter:=E_Param.Text;
end;

procedure TUSActionForm.CBX_CategoryChange(Sender: TObject);
begin
 if CanSetAct then
   FAction.Destination:=CBX_Category.Text;
end;

procedure TUSActionForm.CB_FirstClick(Sender: TObject);
begin
 if CanSetAct then
   FAction.Flag:=CB_First.Checked;
end;

procedure TUSActionForm.FE_FlagChange(Sender: TObject);
begin
 if CanSetAct then
  begin
   if FE_Flag.FilterIndex=1 then
    begin
     if ExtractFileExt(FE_Flag.FileName)='.sem' then
       FAction.Destination:=FE_Flag.FileName
     else
      begin
       FAction.Destination:=FE_Flag.FileName+'.sem';
       CanSetAct:=false;
       FE_Flag.FileName:=FAction.Destination;
       CanSetAct:=true;
      end;
    end
   else
     FAction.Destination:=FE_Flag.FileName;
  end;
end;

procedure TUSActionForm.E_RenameChange(Sender: TObject);
begin
 if CanSetAct then
   FAction.Destination:=E_Rename.Text;
end;

procedure TUSActionForm.CB_SaveNameClick(Sender: TObject);
begin
 if CanSetAct then
   FAction.Flag:=CB_SaveName.Checked;
end;

procedure TUSActionForm.FormDestroy(Sender: TObject);
begin
 FAction.Free;
end;

end.
